<?php
require_once('D:\Yitek\YE\designer/../libs/Validator.class.php');
require_once('D:\Yitek\YE\designer/../libs/db.inc.php');
class User extends \db\DbSet {

    public function validate_Id(){
        if(!$this->_current_) return true;
        $value = isset($this->_current_['Id'])?$this->_current_['Id']:null;
    		return true;
	}

    public function validate_Username(){
        if(!$this->_current_) return true;
        $value = isset($this->_current_['Username'])?$this->_current_['Username']:null;
    		if(!$value) return array('type'=>'required','args'=>1);
		if(strlen($value)>255) return array('type'=>'max','args'=>255);
		$rs = method_exists('\Validator','required')?\Validator::required($value,'1'):true;
		if($rs) return array('type'=>'required','args'=>'1');
		$rs = method_exists('\Validator','min_length')?\Validator::min_length($value,'3'):true;
		if($rs) return array('type'=>'min_length','args'=>'3');
		return true;
	}

    public function validate($allows=null){

        if(!$allows || in_array('Id',$allows)){
            $rs = $this->validate_Id();
            if($rs!==true) $results['Id']=$rs;
        }
        if(!$allows || in_array('Username',$allows)){
            $rs = $this->validate_Username();
            if($rs!==true) $results['Username']=$rs;
        }
    }
	public static function fromArray($data,$prefix=null,$allows=null){
        $fProp_name = $prefix?$prefix.'Id':'Id';
        $id = isset($data[$fProp_name]);
        if(!$id) return null;
    
        if(is_array($prefix)){
            $tmp = $allows;
            $allows =$prefix;
            $prefix = $tmp;
        }
        $obj = new \User();

        $fProp_name =$prefix?$prefix.'Id':'Id';
        if((!$allows || in_array('Id',$allows)) && isset($data[$fProp_name])) $obj->Id=$data[$fProp_name];

        $fProp_name =$prefix?$prefix.'Username':'Username';
        if((!$allows || in_array('Username',$allows)) && isset($data[$fProp_name])) $obj->Username=$data[$fProp_name];

        return $obj;
    }
	static $DB_FIELDNAMES=['Id','Username'];
	static $DB_PRIMARY='Id';
}
